#include "Settings.h"

///Copyright (c) 2009-2010 T. Klepzig 

Settings::Settings()
{
}

bool Settings::Save(Settings::Data data)
{
	BinaryFileHelper bfh("settings.dat", ios::out | ios::binary);

	if (!bfh)
	{
		bfh.Close();
		return false;
	}

	bfh.Exceptions(ios::failbit|ios::badbit);

	try
	{
		#pragma region Language
	
		bfh.Write((char*)&data.lang,sizeof(data.lang));
		
		#pragma endregion

		#pragma region CassiopeiaMain

		bfh.Write((char*)&data.CassiopeiaMainData,sizeof(data.CassiopeiaMainData));
		
		#pragma endregion


		#pragma region DeskNotes

		bfh.Write((char*)&data.DeskNotesData, sizeof(data.DeskNotesData));

		#pragma endregion

		#pragma region Scheduler
		
		bfh.Write((char*)&data.SchedulerData,sizeof(data.SchedulerData));

		#pragma endregion

		#pragma region DeskTidy

		//FreeIDPool
		int freeIDPoolCount=data.DeskTidyData.FreeIDPool.size();
		bfh.Write((char*)&freeIDPoolCount,sizeof(freeIDPoolCount));
		
		list<int>::iterator id;
		for(id = data.DeskTidyData.FreeIDPool.begin();id!=data.DeskTidyData.FreeIDPool.end();id++)
		{
			bfh.Write((char*)&(*id),sizeof(*id));
		}

		//IDCounter
		bfh.Write((char*)&data.DeskTidyData.IDCounter,sizeof(data.DeskTidyData.IDCounter));

		//DeskTidyWndData
		int deskTidyWndDataCount = data.DeskTidyData.DeskTidyWndData.size();
		bfh.Write((char*)&deskTidyWndDataCount,sizeof(deskTidyWndDataCount));

		map<int, DeskTidyWndData>::iterator iter;
		for(iter = data.DeskTidyData.DeskTidyWndData.begin();iter!=data.DeskTidyData.DeskTidyWndData.end();iter++)
		{
			bfh.Write((char*)&iter->first,sizeof(iter->first));
			bfh.Write((char*)&iter->second.Active,sizeof(iter->second.Active));
			bfh.Write((char*)&iter->second.Visible,sizeof(iter->second.Visible));
			bfh.Write((char*)&iter->second.Bounds,sizeof(iter->second.Bounds));
			bfh.Write((char*)&iter->second.Locked,sizeof(iter->second.Locked));
			bfh.Write((char*)&iter->second.Text,sizeof(iter->second.Text));
			

			int extensionCount = iter->second.ExtensionList.size();
			bfh.Write((char*)&extensionCount,sizeof(extensionCount));
		
			set<tstring, TStringSortTemplate>::iterator extension;
			for(extension = iter->second.ExtensionList.begin();extension!=iter->second.ExtensionList.end();extension++)
			{
				bfh.WriteString(*extension);
			}
		}

		#pragma endregion

		/*#pragma region KeyLock
				
		bfh.Write((char*)&data.KeyLockData,sizeof(data.KeyLockData));

		#pragma endregion	*/
		
		#pragma region WindowGroups

		int windowGroupsCount = data.WindowGroupsData.Groups.size();
		bfh.Write((char*)&windowGroupsCount, sizeof(windowGroupsCount));

		map<int, tstring>::iterator group;
		for(group = data.WindowGroupsData.Groups.begin(); group != data.WindowGroupsData.Groups.end(); group++)
		{
			bfh.Write((char*)&group->first, sizeof(group->first));
			bfh.WriteString(group->second);
		}

		bfh.Write((char*)&data.WindowGroupsData.Location, sizeof(data.WindowGroupsData.Location));
		bfh.Write((char*)&data.WindowGroupsData.Opened, sizeof(data.WindowGroupsData.Opened));

		#pragma endregion

		#pragma region DesktopPictureFrame

		bfh.Write((char*)&data.DesktopPictureFrameData, sizeof(data.DesktopPictureFrameData));

		#pragma endregion

		#pragma region QuickButtons

		int functionsCount = data.QuickButtonsData.Functions.size();
		bfh.Write((char*)&functionsCount, sizeof(functionsCount));

		map<int, int>::iterator function;
		for(function = data.QuickButtonsData.Functions.begin(); function != data.QuickButtonsData.Functions.end(); function++)
		{
			bfh.Write((char*)&function->first, sizeof(function->first));
			bfh.Write((char*)&function->second, sizeof(function->second));
		}

		int type1CommandsCount = data.QuickButtonsData.Type1Commands.size();
		bfh.Write((char*)&type1CommandsCount, sizeof(type1CommandsCount));

		map<int, tstring>::iterator command;
		for(command = data.QuickButtonsData.Type1Commands.begin(); command != data.QuickButtonsData.Type1Commands.end(); command++)
		{
			bfh.Write((char*)&command->first, sizeof(command->first));
			bfh.WriteString(command->second);
		}

		bfh.Write((char*)&data.QuickButtonsData.Location, sizeof(data.QuickButtonsData.Location));
		bfh.Write((char*)&data.QuickButtonsData.Opened, sizeof(data.QuickButtonsData.Opened));

		#pragma endregion

		bfh.Close();
	}
	catch(...)
	{
		bfh.Close();
		return false;
	}

	return true;
}

bool Settings::Load(Settings::Data * data)
{
	BinaryFileHelper bfh("settings.dat", ios::in | ios::binary);

	if (!bfh)
	{
		bfh.Close();
		return false;
	}
	
	bfh.Exceptions(ios::failbit | ios::badbit);

	try
	{
		#pragma region Language

		bfh.Read((char*)&data->lang,sizeof(data->lang));
			
		#pragma endregion

		#pragma region CassiopeiaMain

		bfh.Read((char*)&data->CassiopeiaMainData,sizeof(data->CassiopeiaMainData));

		#pragma endregion


		#pragma region DeskNotes

		bfh.Read((char*)&data->DeskNotesData,sizeof(data->DeskNotesData));

		#pragma endregion

		#pragma region Scheduler

		bfh.Read((char*)&data->SchedulerData,sizeof(data->SchedulerData));

		#pragma endregion

		#pragma region DeskTidy

		//FreeIDPool
		int freeIDPoolCount=0;
		bfh.Read((char*)&freeIDPoolCount,sizeof(freeIDPoolCount));
				
		for (int i=0;i<freeIDPoolCount;i++)
		{
			int id=0;
			bfh.Read((char*)&id,sizeof(id));
			data->DeskTidyData.FreeIDPool.push_back(id);
		}


		//IDCounter
		bfh.Read((char*)&data->DeskTidyData.IDCounter,sizeof(data->DeskTidyData.IDCounter));
				
		//DeskTidyWndData
		int deskTidyWndDataCount=0;
		bfh.Read((char*)&deskTidyWndDataCount,sizeof(deskTidyWndDataCount));
				
		int id=0;
		for (int i=0;i<deskTidyWndDataCount;i++)
		{
			bfh.Read((char*)&id,sizeof(id));

			DeskTidyWndData dtWndData;

			bfh.Read((char*)&dtWndData.Active,sizeof(dtWndData.Active));
			bfh.Read((char*)&dtWndData.Visible,sizeof(dtWndData.Visible));
			bfh.Read((char*)&dtWndData.Bounds,sizeof(dtWndData.Bounds));
			bfh.Read((char*)&dtWndData.Locked,sizeof(dtWndData.Locked));
			bfh.Read((char*)&dtWndData.Text,sizeof(dtWndData.Text));

			int extensionCount=0;
			bfh.Read((char*)&extensionCount,sizeof(extensionCount));
					
			for (int j=0;j<extensionCount;j++)
			{
				dtWndData.ExtensionList.insert(bfh.ReadString());
			}
					
			data->DeskTidyData.DeskTidyWndData[id] = dtWndData;
		}

		#pragma endregion

		/*#pragma region KeyLock
				
		bfh.Read((char*)&data->KeyLockData,sizeof(data->KeyLockData));

		#pragma endregion*/

		#pragma region WindowGroups

		int windowGroupsCount = 0;
		bfh.Read((char*)&windowGroupsCount, sizeof(windowGroupsCount));

		for (int i = 0; i < windowGroupsCount; i++)
		{
			int groupID = 0;
			bfh.Read((char*)&groupID, sizeof(groupID));
			data->WindowGroupsData.Groups[groupID] = bfh.ReadString();
		}

		bfh.Read((char*)&data->WindowGroupsData.Location, sizeof(data->WindowGroupsData.Location));
		bfh.Read((char*)&data->WindowGroupsData.Opened, sizeof(data->WindowGroupsData.Opened));

		#pragma endregion

		#pragma region DesktopPictureFrame

		bfh.Read((char*)&data->DesktopPictureFrameData,sizeof(data->DesktopPictureFrameData));

		#pragma endregion

		#pragma region QuickButtons

		int functionsCount = 0;
		bfh.Read((char*)&functionsCount, sizeof(functionsCount));

		for (int i = 0; i < functionsCount; i++)
		{
			int typeId = 0;
			int buttonId = 0;
			bfh.Read((char*)&buttonId, sizeof(buttonId));
			bfh.Read((char*)&typeId, sizeof(typeId));

			data->QuickButtonsData.Functions[buttonId] = typeId;
		}

		int type1CommandsCount = 0;
		bfh.Read((char*)&type1CommandsCount, sizeof(type1CommandsCount));

		for (int i = 0; i < type1CommandsCount; i++)
		{
			int buttonId = 0;
			bfh.Read((char*)&buttonId, sizeof(buttonId));
			data->QuickButtonsData.Type1Commands[buttonId] = bfh.ReadString();
		}

		bfh.Read((char*)&data->QuickButtonsData.Location, sizeof(data->QuickButtonsData.Location));
		bfh.Read((char*)&data->QuickButtonsData.Opened, sizeof(data->QuickButtonsData.Opened));

		#pragma endregion

		bfh.Close();
	}
	catch(...)
	{
		bfh.Close();
		return false;
	}

	return true;
}
